import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class RedisDemoList {
    public static void test1(Jedis jedis) {
        System.out.println("lpush lrange");
        jedis.flushAll();

        jedis.lpush("key", "111","222","333");
        List<String> key = jedis.lrange("key", 0, -1);
        System.out.println(key);
    }

    public static void test2(Jedis jedis) {
        System.out.println("lpush lrange");
        jedis.flushAll();

        jedis.rpush("key", "111","222","333");
        List<String> key = jedis.lrange("key", 0, -1);
        System.out.println(key);
    }

    public static void test3(Jedis jedis) {
        System.out.println("lpop rpop");
        jedis.flushAll();

        jedis.rpush("key", "111","222","333", "444");
        String val1 = jedis.lpop("key");
        String val2 = jedis.lpop("key");
        String val3 = jedis.rpop("key");
        String val4 = jedis.rpop("key");
        System.out.println(val1);
        System.out.println(val2);
        System.out.println(val3);
        System.out.println(val4);
    }

    public static void test4(Jedis jedis) {
        System.out.println("blpop");
        jedis.flushAll();

        //返回结果是一个"二元组", 一个是从哪个 key 对应的 list 中, 一个是删除的元素是什么
        List<String> res = jedis.blpop(100, "key", "key1", "key2");
        System.out.println(res);
    }

    public static void test5(Jedis jedis) {
        System.out.println("brpop");
        jedis.flushAll();

        //返回结果是一个"二元组", 一个是从哪个 key 对应的 list 中, 一个是删除的元素是什么
        List<String> res = jedis.brpop(100, "key", "key1", "key2");
        System.out.println(res);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            test5(jedis);
        }
    }
}
